package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IRegisterService;
import com.mall.user.IVerifyService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.KaptchaCodeRequest;
import com.mall.user.dto.KaptchaCodeResponse;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/user")
public class RegisterController {

    //调用验证码服务
    @Reference(check = false,timeout = 2000,retries = 0)
    private IKaptchaService kaptchaService;

    //调用注册服务
    @Reference(check = false,timeout = 2000,retries = 0)
    private IRegisterService registerService;

    @Anoymous
    @PostMapping("/register")
    public ResponseData register(@RequestBody Map<String,String> map, HttpServletRequest request){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        //校验验证码
        ResponseData responseData = kaptcha(captcha,request);
        if (responseData != null){
            return responseData;
        }

        //向用户注册表中插入一条记录
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setEmail(email);
        userRegisterRequest.setUserPwd(userPwd);
        UserRegisterResponse register = registerService.register(userRegisterRequest);
        if (!register.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            //插入失败
            return new ResponseUtil<>().setErrorMsg(register.getMsg());
        }
        return new ResponseUtil<>().setData(null);
    }

    //校验验证码
    public ResponseData kaptcha(String captcha,HttpServletRequest request){
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setCode(captcha);
        //从cookie获得uuid
        String uuid = CookieUtil.getCookieValue(request,"kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            //验证码不正确
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return null;
    }

    @Reference(check = false)
    IVerifyService iVerifyImpl;
    //邮件验证
    @Anoymous
    @GetMapping("verify")
    public ResponseData verify(String uid,String username){
        iVerifyImpl.verify(uid,username);
        return new ResponseUtil<>().setData(null);
    }
}
